package cn.sumpu.toolkit.common.http;

import java.nio.charset.Charset;
import java.util.Map;
import java.util.Set;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpVersion;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.ByteArrayBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.CoreProtocolPNames;
import org.apache.http.util.EntityUtils;

/**
 * HTTP代理.
 * 
 * @author nick
 * 
 */
public class HttpProxy {
	/**
	 * 发送GET请求.
	 * 
	 * @param url
	 *            请求URL
	 * @return 响应结果
	 * @throws Exception
	 *             请求失败
	 */
	public static String doGet(String url) throws Exception {
		HttpClient httpclient = new DefaultHttpClient();
		HttpGet httpGet = new HttpGet(url);
		HttpResponse response = httpclient.execute(httpGet);
		HttpEntity resEntity = response.getEntity();
		String ret = null;
		if (null != resEntity) {
			String charset = EntityUtils.getContentCharSet(resEntity);
			if (null == charset) {
				charset = "UTF-8";
			}
			ret = EntityUtils.toString(resEntity, charset);

		}
		httpclient.getConnectionManager().shutdown();
		return ret;
	}

	/**
	 * 发送POST请求.
	 * 
	 * @param url
	 *            请求URL
	 * @param params
	 *            请求参数
	 * @return 响应结果
	 * @throws Exception
	 *             请求失败
	 */
	public static String doPost(String url, Map<String, String> params)
			throws Exception {
		HttpClient httpclient = new DefaultHttpClient();
		httpclient.getParams().setParameter(
				CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
		HttpPost httpPost = new HttpPost(url);
		MultipartEntity multipartEntity = new MultipartEntity();
		Set<String> keySet = params.keySet();
		for (String key : keySet) {
			multipartEntity.addPart(key, new StringBody(params.get(key),
					Charset.forName("UTF-8")));
		}
		httpPost.setEntity(multipartEntity);
		HttpResponse response = httpclient.execute(httpPost);
		HttpEntity resEntity = response.getEntity();
		String ret = null;
		if (null != resEntity) {
			String charset = EntityUtils.getContentCharSet(resEntity);
			if (null == charset) {
				charset = "UTF-8";
			}
			ret = EntityUtils.toString(resEntity, charset);
		}
		httpclient.getConnectionManager().shutdown();
		return ret;
	}

	/**
	 * 通过HTTP协议把图片数据发送到指定的图片服务器上.
	 * 
	 * @param serverUrl
	 *            图片服务器地址
	 * @param savePath
	 *            图片在图片服务器上的保存地址
	 * @param saveFilename
	 *            图片在图片服务器上的保存文件名
	 * @param imageData
	 *            图片数据
	 * @return ok-成功；error-失败
	 * @throws Exception
	 *             发送图片失败
	 */
	public static String sendImage(String serverUrl, String savePath,
			String saveFilename, byte[] imageData) throws Exception {
		HttpClient httpclient = new DefaultHttpClient();
		httpclient.getParams().setParameter(
				CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
		HttpPost httppost = new HttpPost(serverUrl);

		MultipartEntity multipartEntity = new MultipartEntity();
		multipartEntity.addPart("savePath",new StringBody(savePath, Charset.forName("UTF-8")));
		multipartEntity.addPart("saveFilename", new StringBody(saveFilename,Charset.forName("UTF-8")));
		multipartEntity.addPart("pic",new ByteArrayBody(imageData, "image.jpg"));
		httppost.setEntity(multipartEntity);

		HttpResponse response = httpclient.execute(httppost);
		HttpEntity resEntity = response.getEntity();

		String ret = null;
		if (resEntity != null) {
			String charset = EntityUtils.getContentCharSet(resEntity);
			ret = EntityUtils.toString(resEntity, charset);
		}

		httpclient.getConnectionManager().shutdown();

		return ret;
	}

	/**
	 * 通过HTTP协议删除指定的图片服务器上的图片文件.
	 * 
	 * @param serverUrl
	 *            图片服务器地址
	 * @param imageFile
	 *            要删除的图片在图片服务器上的保存文件名
	 * @return ok-成功；error-失败
	 * @throws Exception
	 *             删除图片失败
	 */
	public static String deleteImage(String serverUrl, String imageFile)
			throws Exception {
		HttpClient httpclient = new DefaultHttpClient();
		httpclient.getParams().setParameter(
				CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
		HttpPost httppost = new HttpPost(serverUrl);

		MultipartEntity multipartEntity = new MultipartEntity();
		multipartEntity.addPart("imageFile",
				new StringBody(imageFile, Charset.forName("UTF-8")));
		httppost.setEntity(multipartEntity);

		HttpResponse response = httpclient.execute(httppost);
		HttpEntity resEntity = response.getEntity();

		String ret = null;
		if (resEntity != null) {
			String charset = EntityUtils.getContentCharSet(resEntity);
			ret = EntityUtils.toString(resEntity, charset);
		}

		httpclient.getConnectionManager().shutdown();

		return ret;
	}

	

}
